package com.morgan.storageperlib.saf

import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.net.Uri
import androidx.activity.result.ActivityResult
import androidx.activity.result.contract.ActivityResultContract
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity

/**
 *description：读取bitmap
 *<br>author：caowugao
 *<br>time：2021/3/8 0008 16:22
 */
class ReadBitmapHandler(act : AppCompatActivity, contract : ActivityResultContract<Intent, ActivityResult> = ActivityResultContracts.StartActivityForResult(), callback : (Boolean, Uri?, Bitmap?) -> Unit) : ReadFileHandler(act, contract, {success, uri, input ->
    if (!success) {
        callback.invoke(false, null, null)
    } else {
        try {
            callback.invoke(true, uri, BitmapFactory.decodeStream(input))
        } catch (e : Throwable) {
            e.printStackTrace()
            callback.invoke(false, null, null)
        }
    }
}) {
}